<?php
define('PATH_TO_XML_FILE', 'Xml/Db/users.xml');
define('NEW_ID', -1);
$act = isset($_REQUEST['act']) ? $_REQUEST['act'] : '';
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : NEW_ID;
switch ($act){
	case 'del':
		deletUser($id);
		break;
	case 'add':
		addUser($id);
		break;
	case 'update':
		updateUser($id);
		break;
	case 'save':
		save($_POST);
		break;
	default:
}

function save(array $dataToSave){
	//need to validate
	//var_dump($dataToSave);die;
	$userId = $dataToSave['id'];
	$isNewUser = (bool)($userId == NEW_ID);
	$users = new SimpleXMLElement(PATH_TO_XML_FILE, null, true);
	if($isNewUser){
		$lastUser = $users->xpath('/users/user[last()]');
		//דרך שקולה
		//$users->user[$users->count()-1]['id']
		//var_dump($lastUser);die;
		//var_dump($users->user[$users->count()-1]['id']);die;
		//$users->user[$users->count()-1]['id']
		$userId = (int)$lastUser[0]['id']+1;
		$user = $users->addChild('user');
		$user->addAttribute('id', $userId);
		$user->addChild('firstName', $dataToSave['firstName']);
		$user->addChild('lastName', $dataToSave['lastName']);
		$user->addChild('email', $dataToSave['email']);
	}else{
		$user = $users->xpath('/users/user[@id="'.$userId.'"]');
		if($user){
			$user = $user[0];
			$user->firstName = $dataToSave['firstName'];
			$user->lastName = $dataToSave['lastName'];
			$user->email = $dataToSave['email'];
		}
	}
	
	$dom = new DOMDocument();
	$dom->preserveWhiteSpace = false;
	$dom->formatOutput = true;
	$dom->loadXML($users->asXML());
	$dom->save(PATH_TO_XML_FILE);
	
	header("Location:".str_replace('?act=save', '', $_SERVER['PHP_SELF']));
}

function deletUser($userId){
	
	$dom = new DOMDocument();
	$dom->load(PATH_TO_XML_FILE);
	
	//create xpath for query 
	$xpath = new DomXPath($dom);
	$result = $xpath->query('//user[@id="'.$userId.'"]');
	if($result->length == 1){
		//root element
		$users = $dom->documentElement;
		
		//delete the child from root node
		$users->removeChild($result->item(0));
		$dom->save(PATH_TO_XML_FILE);
	}
}

function form(array $data = array()){
	?>
		<form method="post"  action="<?php echo $_SERVER['PHP_SELF'];?>?act=save" >
			<input type="hidden"  value="<?php echo isset($data['id']) ? $data['id']: '';?>"   name="id" />
			<div>
				<label>first name</label>
				<input type="text"  value="<?php echo isset($data['firstName']) ? $data['firstName']: '';?>"  name="firstName" />
			</div>
			<div>
				<label>last name</label>
				<input type="text"  value="<?php echo isset($data['lastName']) ? $data['lastName']: '';?>"  name="lastName" />
			</div>
			<div>
				<label>email</label>
				<input type="text"  value="<?php echo isset($data['email']) ? $data['email']: '';?>"  name="email" />
			</div>
			<div>
				<input type="submit"  value="Save"  />
			</div>
		</form>
	<?php
}

function addUser($userId){
	form(array('id' => $userId));
	die;
}

function updateUser($userId){
	$users = new SimpleXMLElement(PATH_TO_XML_FILE, null, true);
	$user = $users->xpath('/users/user[@id="'.$userId.'"]');
	
	if($user){
		$user = (array)$user[0];
	}
	$user['id'] = $userId;
	form($user);
	die;
}


$xml = new SimpleXMLElement(PATH_TO_XML_FILE, null, true);

?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>?act=add" >Add</a><br />
<table border="1"   >
	<thead>
		<tr>
			<th>firstName</th>
			<th>lastName</th>
			<th>email</th>
			<th>#</th>
			<th>#</th>
		</tr>
	</thead>
	<tbody>
		<?php
			foreach ($xml->user as $user){
				//var_dump($user); die;
				?><tr>
					<td><?php echo $user->firstName;?></td>
					<td><?php echo $user->lastName;?></td>
					<td><?php echo $user->email;?></td>
					<td><a href="<?php echo $_SERVER['PHP_SELF']?>?act=update&id=<?php echo $user['id'];?>" >update</a></td>
					<td><a href="<?php echo $_SERVER['PHP_SELF']?>?act=del&id=<?php echo $user['id'];?>" >delete</a></td>
				</tr><?php
			}
		?>
	</tbody>
</table>
<?php 